package com.magist.core.http;

import com.magist.core.constants.AppParams;
import com.magist.core.enums.BaseEnum; import com.magist.core.util.TxtUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * http线程处理
 * 
 * @author zhuangja
 *
 */
public class HttpHolder {
	/**
	 * 获取原生httprequest
	 * 
	 * @return
	 */
	public static HttpServletRequest getHttpRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 获取原生httpresponse
	 * 
	 * @return
	 */
	public static HttpServletResponse getHttpResponse() {
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}

	/**
	 * 获取清求参数
	 * 
	 * @param param
	 * @return
	 */
	public static Object getRequestParam(String param) {
		return getHttpRequest().getParameter(param);
	}
	/**
	 * 查获cookies
	 * @param param
	 * @return
	 */
	public static String getCookie(String param) {
		Cookie[] cks = getHttpRequest().getCookies();
		if(cks!=null){
			for (Cookie cki : cks) {
				if (cki.getName().equalsIgnoreCase(param))
					return cki.getValue();
			}
		}
		return "";
	}
	
	/**
	 * 设置cookies
	 * @param param
	 * @param value
	 */
	public static void setCookie(String param,String value) {
		Cookie ck = new Cookie(param, value);
		ck.setPath("/");
		ck.setMaxAge(AppParams.COOKIE_KEEP_TIME);
		getHttpResponse().addCookie(ck);
	}
	/**
	 * 获取cookie值
	 * 
	 * @param domain
	 * @param param
	 * @return
	 */
	public static String getCookie(String domain, String param) {
		Cookie[] cks = getHttpRequest().getCookies();
		for (Cookie cki : cks) {
			String t= TxtUtil.isEmpty(cki.getDomain())?"":cki.getDomain();
				
			if (t.equalsIgnoreCase(domain) && (cki.getPath() + cki.getName()).equalsIgnoreCase(param))
				return cki.getValue();
		}
		return "";
	}

	/**
	 * 设置cookie值
	 * 
	 * @param domain
	 * @param param
	 * @param value
	 */
	public static void setCookie(String domain, String param, String value) {
		Cookie ck = new Cookie(param, value);
		ck.setDomain(domain);
		ck.setPath("/");
		ck.setMaxAge(AppParams.COOKIE_KEEP_TIME);
		getHttpResponse().addCookie(ck);
	}

	/**
	 * 得到url
	 * @return
	 */
	public static String getUrl() {
		if (getHttpRequest() != null) {
			String p = getHttpRequest().getScheme() + "://" + getHttpRequest().getServerName();
			if (getHttpRequest().getServerPort() != 80)
				p = p + ":" + getHttpRequest().getServerPort();
			p = p + getHttpRequest().getContextPath();
			return p;
		} else
			return "";
	}
	/**
	 * 得到清求路径
	 * @return
	 */
	public static String getPath() {
		if (getHttpRequest() != null) {
			String p = getHttpRequest().getContextPath();
			return p;
		} else
			return "";
	}
	/**
	 * 获取路径
	 * @return
	 */
	public static String getBasePath() {
		return getContextPath();
	}

	/**
	 * 得到当前项目路径
	 * @return
	 */
	public static String getContextPath() {
		if (getHttpRequest() != null) {
			String p = getHttpRequest().getScheme() + "://" + getHttpRequest().getServerName();
			if (getHttpRequest().getServerPort() != 80)
				p = p + ":" + getHttpRequest().getServerPort();
			p = p + getHttpRequest().getContextPath();
			return p;
		} else
			return "";
	}
}
